package me.wangqu.weiyouqu.web.rest.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by joln on 2016/7/19.
 */
public class UrlUtil {
    public final static String STARTHTML = "/html/";
    public final static String ENDHTML = ".html";

    public static final class Urls {
        public final static String URLTOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token";
        public static final String POST_REQUEST = "POST";
        public static final String GET_REQUEST = "GET";
        public final static String URLINFO = "https://api.weixin.qq.com/sns/userinfo";

    }

    public final static String getUrl(String link, String date, int id, int type, int code) {
        return "http://" + link + STARTHTML + date + "/1" + (id + 12345) + type + code + ENDHTML;

    }

    public String getImg(String rs, String type) {
        ArrayList<String> imgLists = new ArrayList<>();
        try {

            JSONObject object = new JSONObject(rs);
            JSONArray array = object.getJSONArray("imglist");
            for (int i = 0; i < array.length(); i++) {
                if (!"".equals(array.getString(i))) {
                    imgLists.add(array.getString(i) + type);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JSONArray array = new JSONArray();
        for (int i = 0; i < imgLists.size(); i++) {
            array.put(imgLists.get(i));
        }
        JSONObject object = new JSONObject();
        try {
            object.put("imglist", array);
            return object.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
